package com.hankol.material.help

import android.app.Activity
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.*
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import com.baselibrary.Constants
import com.baselibrary.router.ActivityRouter
import com.hankol.material.R
import com.hankol.material.viewmodel.MaterialBaseViewModel

object PopUtils {

    /**
     *  已进入下载列队 请勿关闭软件
     * @param activity
     *：isAlwayDownload：true已下载过， 跳转到已完成列表
     */
    @JvmStatic
    fun showJoinDownloadDialog(activity: Activity, isAlwayDownload: Boolean = false): PopupWindow {
        val downloadFileView =
            LayoutInflater.from(activity).inflate(R.layout.popup_join_download, null)
        val popupWindow =
            PopupWindow(downloadFileView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        val lp = activity.window.attributes
        lp.alpha = 0.5f
        activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        activity.window.attributes = lp
        val tvSee = downloadFileView.findViewById<TextView>(R.id.tv_see)
        downloadFileView.findViewById<LinearLayout>(R.id.ll_root).setOnClickListener {
            if (popupWindow.isShowing) popupWindow.dismiss()
        }
        tvSee.setOnClickListener {
            //进入我的下载
            val bundle = Bundle()
            bundle.putInt(Constants.KEY_SWITCH_POS, if (isAlwayDownload) 1 else 0)//已下载过的跳转到已完成列表
            ActivityRouter.startActivity(ActivityRouter.MATERIAL.MATERIAL_DOWNLOAD_MAIN, bundle)
            if (popupWindow.isShowing) popupWindow.dismiss()
        }
        popupWindow.setBackgroundDrawable(BitmapDrawable())
        popupWindow.isOutsideTouchable = true//点击外部消失
        popupWindow.isFocusable = true// 取得焦点
        popupWindow.isTouchable = true//设置可以点击
        popupWindow.animationStyle = R.style.camera_popwindow_anim_style
        popupWindow.setOnDismissListener {
            val lpWindow = activity.window.attributes
            lpWindow.alpha = 1f
            activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            activity.window.attributes = lpWindow
        }
        popupWindow.showAtLocation(downloadFileView, Gravity.BOTTOM, 0, 0)
        tvSee.postDelayed(object : Runnable {
            override fun run() {
                if (popupWindow.isShowing) popupWindow.dismiss()
            }
        }, 2000)
        return popupWindow
    }

    /**
     * 左右按钮 弹窗
     * @param activity
     *
     */
    fun showRightLeftDialog(activity: Activity, content: String, callBack: MaterialBaseViewModel.IRightLeftCallBack, leftStr: String = "取消", rightStr: String = "确认"): PopupWindow {
        val downloadFileView =
            LayoutInflater.from(activity).inflate(R.layout.popup_right_left, null)
        val popupWindow =
            PopupWindow(downloadFileView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        val lp = activity.window.attributes
        lp.alpha = 0.5f
        activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        activity.window.attributes = lp
        val tvleft = downloadFileView.findViewById<TextView>(R.id.tv_det_cancel)
        tvleft.setOnClickListener {
            if (popupWindow.isShowing) popupWindow.dismiss()
            callBack.leftClick()
        }
        tvleft.text = "${leftStr}"
        downloadFileView.findViewById<TextView>(R.id.tv_content).text = content
        val tvRight = downloadFileView.findViewById<TextView>(R.id.tv_det_confirm)
        tvRight.text = "${rightStr}"
        tvRight.setOnClickListener {
            callBack.rightClick()
            if (popupWindow.isShowing) popupWindow.dismiss()
        }
        popupWindow.setBackgroundDrawable(BitmapDrawable())
        popupWindow.isOutsideTouchable = true//点击外部消失
        popupWindow.isFocusable = true// 取得焦点
        popupWindow.isTouchable = true//设置可以点击
        popupWindow.animationStyle = R.style.camera_popwindow_anim_style
        popupWindow.setOnDismissListener {
            val lpWindow = activity.window.attributes
            lpWindow.alpha = 1f
            activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            activity.window.attributes = lpWindow
        }
        popupWindow.showAtLocation(downloadFileView, Gravity.CENTER, 0, 0)
        return popupWindow
    }

    /**
     * 左右按钮 弹窗
     * @param activity
     *
     */
    fun showMaterialVideoLoad(activity: Activity, pcSize: String, onClickView: View.OnClickListener): PopupWindow {
        val downloadFileView =
            LayoutInflater.from(activity).inflate(R.layout.popup_material_downvideo, null)
        val popupWindow =
            PopupWindow(downloadFileView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        val lp = activity.window.attributes
        lp.alpha = 0.5f
        activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        activity.window.attributes = lp
        downloadFileView.findViewById<TextView>(R.id.tv_file_videoPcDown).text = "复制下载链接 $pcSize"
        downloadFileView.findViewById<ImageView>(R.id.iv_file_downVideoCancel).setOnClickListener {
            if (popupWindow.isShowing) popupWindow.dismiss()
        }
        downloadFileView
            .findViewById<LinearLayout>(R.id.ll_file_videoPhoneDown)
            .setOnClickListener {
                onClickView.onClick(it)
                if (popupWindow.isShowing) popupWindow.dismiss()
                //下载
            }
        downloadFileView.findViewById<LinearLayout>(R.id.ll_file_videoPcDow).setOnClickListener {
            onClickView.onClick(it)
            if (popupWindow.isShowing) popupWindow.dismiss()
            //复制剪切板
        }
        popupWindow.setBackgroundDrawable(BitmapDrawable())
        popupWindow.isOutsideTouchable = true//点击外部消失
        popupWindow.isFocusable = true// 取得焦点
        popupWindow.isTouchable = true//设置可以点击
        popupWindow.animationStyle = R.style.camera_popwindow_anim_style
        popupWindow.setOnDismissListener {
            val lpWindow = activity.window.attributes
            lpWindow.alpha = 1f
            activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            activity.window.attributes = lpWindow
        }
        popupWindow.showAtLocation(downloadFileView, Gravity.BOTTOM, 0, 0)
        return popupWindow
    }

}